home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / Drag_on Modules / hAWK programs / XRef.T < prev   
Encoding:
Text File  |  1993-02-08  |  6.2 KB  |  286 lines  |  [TEXT/KEEN]

  1. # XRef.T (or .A): from one or more source files, build a cross-reference
  2. # list of selected top-level terms.
  3. #
  4. ############## NOT AN EXECUTABLE FILE ###################
  5. #
  6. #this file is used only as a "Template" by
  7. #$XRef, which is the one to call for cross-referencing.
  8. #The file "Skip for XRef" below holds a list of common terms to skip over.
  9. #In large cross-reference jobs it may be NECESSARY to skip the commonest
  10. #terms to avoid overflowing the list of references.
  11. #
  12. #The tedious bit: stripping comments and strings (and character constants).
  13. # A line can begin within string or comment, contain several of each,
  14. # and end in the midst of a string or comment - whole line can
  15. # be within a single string or comment for that matter.
  16. # inCom == we are in a comment
  17. # inStr == in a string
  18.  
  19. BEGIN {
  20.         skipfile = STDPATH "Drag_on Modules:hAWK programs:" "Skip for XRef"
  21.         while (getline < skipfile > 0)
  22.             {
  23.             for ( k = 1; k <= NF; k++)
  24.                 skipList[$k] = 1; #Forces skipList[$k] to "exist".
  25.             }
  26.         close(skipfile)
  27.         $0 = ""
  28.         progressFile = STDPATH "$tempProgress"
  29.         }
  30.  
  31. FNR == 1 {    
  32.         if (inCom || inStr)
  33.             {
  34.             if (inCom)
  35.                 errString = "Unbalanced comment in " FILENAME " - please fix."
  36.             else
  37.                 errString = "Unbalanced string in " FILENAME " - please fix."
  38.             do_exit()
  39.             }
  40.         z = split(FILENAME, names, ":")
  41.         fName[++fIndex] = names[z]
  42.         if (!progress("\nXReffing: " names[z]))
  43.             { # concurrent mode, print progress to file
  44.             print "XReffing:", names[z] > progressFile
  45.             close(progressFile)
  46.             }
  47.         }
  48.  
  49.     {
  50.     if (inCom) #starting in comment
  51.         SkipWhileInComment(); #or stop at end of line
  52.     else if (inStr) #starting in string
  53.         SkipWhileInString(); #or stop at end of line
  54.     if (!inCom && !inStr &&
  55.         (index($0, "/*") || index($0, "\"") || index($0, "'")))
  56.         SkipComsAndStrs(); # - and ticks, or stop at end of line
  57.     #finally, strip C++-style line-end comments
  58.     if (!inCom && !inStr && (startPlus = index($0, "//")))
  59.         $0 = substr($0, startPlus-1)
  60.     }
  61.  
  62. #Strip down to words, look up term and record locations.
  63. #Only the file index is recorded - at end when printing, the index
  64. #is replaced by the proper file name.
  65.     {
  66.     gsub(/[^A-Za-z0-9_]/, " ")
  67.     for (i = 1; i <= NF; ++i)
  68.         {
  69.         #skip if in skipList
  70.         if ($i in skipList)
  71.             continue;
  72.         type = lookup($i)
  73.         ##MARK record
  74.         #if (type == 1) #define
  75.         #        defines[$i] = defines[$i] "\t" fIndex "\t" FNR
  76.         #else if (type == 2)#variable
  77.         #        vars[$i] = vars[$i] "\t" fIndex "\t" FNR
  78.         #etc
  79.         }
  80.     }
  81.  
  82. END {
  83.     if (noGo == 1)
  84.         exit;
  85.     if (!progress("\nBuilding final list..."))
  86.         { # concurrent mode, print progress to file
  87.         print "Building final list..." > progressFile
  88.         close(progressFile)
  89.         }
  90.     print "Cross-reference listing:"
  91.     #build master array, for sorting all at once
  92.     ##MARK linear
  93.     #for (w in defines)
  94.     #    {
  95.     #      linear[++m] = w ": #define" defines[w]
  96.     #    delete defines[w]
  97.     #      }
  98.     #for (w in vars)
  99.     #    {
  100.     #      linear[++m] = w ": variable" vars[w]
  101.     #    delete vars[w]
  102.     #      }
  103.     #etc
  104.     if (!progress("\nSorting final list..."))
  105.         { # concurrent mode, print progress to file
  106.         print "Sorting final list..." > progressFile
  107.         close(progressFile)
  108.         }
  109.     sort(linear, ind, "d")
  110.     if (!progress("\nPrinting final list to stdout..."))
  111.         { # concurrent mode, print progress to file
  112.         print "Printing final list to stdout..." > progressFile
  113.         close(progressFile)
  114.         }
  115.      for (j = 1; j <= m; ++j)
  116.         {
  117.         r = split(linear[ind[j]], out, "\t")
  118.         print out[1]
  119.         for (t = 2; t <= r; ++t)
  120.             {#print file name from array fName, print line number
  121.             print "\t", fName[out[t]], out[++t]
  122.             }
  123.         print ""
  124.         }
  125.     if (m == 0)#nothing at all
  126.         print "No top-level terms were found."
  127.     }
  128.  
  129. function SkipWhileInComment(        len, i)
  130.     {
  131.     len = length()#note $0 is default argument for length()
  132.     for (i = 1; i <= len && inCom; ++i)
  133.         {
  134.         if (substr($0,i,1) == "*" && substr($0,i+1,1) == "/")
  135.             inCom = 0;
  136.         }
  137.     if (inCom)#comment continues on next line
  138.         $0 = ""
  139.     else #strip out the comment from the start of $0
  140.         $0 = substr($0,i+2)
  141.     }
  142.  
  143. function SkipWhileInString(        len, i, c, esc)
  144.     {
  145.     esc = 0
  146.     len = length()
  147.     for (i = 1; i <= len && inStr; ++i)
  148.         {
  149.         c = substr($0,i,1)
  150.         if (c == "\"")
  151.             {
  152.             if (esc == 0 || esc%2 == 0)
  153.                 inStr = 0;
  154.             else
  155.                 esc = 0
  156.             }
  157.         else if (c == "\\")
  158.             ++esc
  159.         else
  160.             esc = 0
  161.         }
  162.     if (inStr)
  163.         {
  164.         if (c != "\\")
  165.             {
  166.             errString = "Unbalanced string at " FILENAME " " FNR " - please fix."
  167.             do_exit()
  168.             }
  169.         $0 = ""
  170.         }
  171.     else
  172.         $0 = substr($0,i+1)
  173.     }
  174.  
  175. #We know we're not starting within a comment or string,
  176. #and line may contain comment or string start.
  177. function SkipComsAndStrs(    c, esc, i, len)
  178.     {
  179.     
  180.     len = length()
  181.     for (i = 1; i <= len && !inCom && !inStr; ++i)
  182.         {
  183.         c = substr($0,i,1)
  184.         if (c == "/" && substr($0,i+1,1) == "*")
  185.             {
  186.             inCom = 1;
  187.             len -= StripComment(i, len);
  188.             }
  189.         else if (c == "\"")
  190.             {
  191.             inStr = 1;
  192.             len -= StripString(i, len);
  193.             }
  194.         else if (c == "'")
  195.             {
  196.             len -= StripTicks(i, len);
  197.             }
  198.         }
  199.     }
  200.  
  201. function StripComment(start, len,    i)
  202.     {
  203.     for (i = start+2; i <= len && inCom; ++i)
  204.         {
  205.         if (substr($0,i,1) == "*" && substr($0,i+1,1) == "/")
  206.             inCom = 0;
  207.         }
  208.     if (inCom)#comment continues on next line
  209.         $0 = substr($0,1,start-1)
  210.     else #strip out the comment from the middle of $0
  211.         $0 = substr($0,1,start-1) substr($0,i+2)
  212.     return i - start + 2
  213.     }
  214.  
  215. function StripString(start, len,    i,c,esc)
  216.     {
  217.     esc = 0
  218.     for (i = start+1; i <= len && inStr; ++i)
  219.         {
  220.         c = substr($0,i,1)
  221.         if (c == "\"")
  222.             {
  223.             if (esc == 0 || esc%2 == 0)
  224.                 inStr = 0;
  225.             else
  226.                 esc = 0
  227.             }
  228.         else if (c == "\\")
  229.             ++esc
  230.         else
  231.             esc = 0
  232.         }
  233.     if (inStr)
  234.         {
  235.         if (c != "\\")
  236.             {
  237.             errString = "Unbalanced string at " FILENAME " " FNR " - please fix."
  238.             do_exit()
  239.             }
  240.         $0 = substr($0,1,start-1)
  241.         }
  242.     else
  243.         $0 = substr($0,1,start-1) substr($0,i+1)
  244.     return i - start + 1
  245.     }
  246.  
  247. function StripTicks(start, len,        i,c,esc)
  248.     {
  249.     esc = 0
  250.     for (i = start+1; i <= len; ++i)
  251.         {
  252.         c = substr($0,i,1)
  253.         if (c == "'")
  254.             {
  255.             if (esc == 0 || esc%2 == 0)
  256.                 break;
  257.             else
  258.                 esc = 0
  259.             }
  260.         else if (c == "\\")
  261.             ++esc
  262.         else
  263.             esc = 0
  264.         }
  265.     if (i > len)
  266.         {
  267.         errString = "Unbalanced ticks at " FILENAME " " FNR " - please fix."
  268.         do_exit()
  269.         }
  270.         $0 = substr($0,1,start-1) substr($0,i+1)
  271.     return i - start + 1
  272.     }
  273.  
  274. function do_exit()
  275.     {
  276.     prompt(errString)
  277.     print ""
  278.     print ""
  279.     print "------------ left off at ----------"
  280.     print
  281.     print "--- " errString
  282.     print errString > "stderr"
  283.     noGo = 1
  284.     exit
  285.     }
  286.